/**
 * Created by wengms on 2016/8/12.
 */
define([
    "angular",
    "framework/utils/JSONPrettyFormat",
    'framework/dataGrid/DataGridModule',
    'framework/pagination/GillionPaginationModule',
    'framework/msg/GillionMsgModule',
    'angular-strap-tpl'], function (angular,
                                    JSONPrettyFormat) {

    var UnitCaseModule = angular.module("UnitCaseModule", [
        'DataGridModule',
        'GillionPaginationModule',
        'GillionMsgModule',
        'mgcrea.ngStrap']);

    UnitCaseModule.controller("UnitCaseController", function ($scope, Resource, $dataSourceManager, $modal, $location) {
        var CMD = {
            ADD: "ADD",
            EDIT: "EDIT",
            VIEW: "VIEW"
        }, UnitCases = Resource("/grule-manager/unitcases/:id", {
            id: "@id"
        }), crudTpl = $modal
        ({
            scope: $scope,
            contentTemplate: '/html/unitcases/unitcase-crud',
            placement: "center",
            show: false
        }),rulePackageInfo=$location.search();

        $scope.defaultParams =rulePackageInfo;
        $scope.item = {};
        angular.extend($scope.item,rulePackageInfo);

        $scope.settings = {
            editing: true,
            cmd: CMD.ADD,
            icon: "glyphicon glyphicon-plus"
        };

        $scope.actions = {
            showDetail: function (row) {
                $scope.settings = {
                    editing: false,
                    cmd: CMD.VIEW,
                    icon: "glyphicon glyphicon-list"
                };
                $scope.item = row;
                crudTpl.show();
            },
            showUpdateView: function (row) {
                $scope.settings = {
                    editing: true,
                    cmd: CMD.EDIT,
                    icon: "glyphicon glyphicon-edit"
                };
                $scope.item = row;
                crudTpl.show();
            },
            showAddView: function () {
                $scope.settings = {
                    editing: true,
                    cmd: CMD.ADD,
                    icon: "glyphicon glyphicon-plus"
                };
                $scope.item = {};
                angular.extend($scope.item,rulePackageInfo);
                crudTpl.show();
            },

            create: function () {
                UnitCases.create($scope.item, function (result) {
                    if (result.success) {
                        alert("用例创建成功.");
                        $scope.actions.refresh();
                    }
                    crudTpl.hide();
                });
            },
            update: function () {
                UnitCases.update($scope.item, function (result) {
                    if (result.success) {
                        alert("用例修改成功.");
                        $scope.actions.refresh();
                    } else {
                        alert("用例修改失败." + result.errorMessages);
                    }
                    crudTpl.hide();
                });
            },
            refresh: function () {
                $dataSourceManager.dataSources["UnitCaseSource"].doRequestData($scope.defaultParams);
            },
            remove: function (row) {
                UnitCases.remove(row, function (result) {
                    if (result.success) {
                        alert("用例删除成功.");
                        $scope.actions.refresh();
                    } else {
                        alert("用例删除失败." + result.errorMessages);
                    }
                })
            },
            paramsManager: function (row) {
                window.open("/html/unitcases/params/manager#?unitCaseId=" + row.id);
            },
            mockManager: function (row) {
                window.open("/html/unitcases/mocks/manager#?unitCaseId=" + row.id);
            },
            expectManager: function (row) {
                window.open("/html/unitcases/expects/manager#?unitCaseId=" + row.id);
            },
        };


        $scope.saveUnitCase = function () {
            if ($scope.settings.cmd === CMD.ADD) {
                $scope.actions.create();
            } else if ($scope.settings.cmd === CMD.EDIT) {
                $scope.actions.update();
            }
        };
    });


    return UnitCaseModule;
});